package com.obas.monitor;

import java.util.Date;
import java.util.List;

import com.obas.access.business.service.TestTaskAccessService;
import com.obas.access.business.service.TestTaskItemAccessService;
import com.obas.business.common.vo.TestContext;
import com.obas.model.business.TestPlanItem;
import com.obas.model.business.TestTask;
import com.obas.model.business.TestTaskItem;

public class TestPlanPlayer extends Thread {
	
   public static String LOADER_HTML = "agent/core/autloader.html";

   private TestContext context;
   private List<TestPlanItem> items;
   private TestTaskItemAccessService taskItemService;
   private TestTaskAccessService taskService;

   public TestPlanPlayer(TestContext context, List<TestPlanItem> items, TestTaskAccessService taskService, TestTaskItemAccessService taskItemService) {
       this.context = context;
       this.items = items;
       this.taskItemService = taskItemService;
       this.taskService = taskService;
   }

   public void run() {
      try {
         TestTask task = taskService.findById(context.testTaskId);
         task.setStartTime(new Date());
         taskService.update(task);
         runTestPlanItems();
         task = taskService.findById(context.testTaskId);
         task.setEndTime(new Date());
         taskService.update(task);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

    private void runTestPlanItems() {
       while (items != null && items.size() > 0) {
           synchronized (context) {
               TestPlanItem item = items.remove(0);
               context.ideState = AtCommand.ideRunning;
               context.testPlanItemId = item.getId();
               context.parameterId = item.getParameterId();
               context.scriptId = item.getTestcaseScript().getId();
               String autURL = item.getTestcaseScript().getAutEndpoint();
               String url = "http://127.0.0.1:9000/itest/" + LOADER_HTML + "?debug=true&autURL=" + autURL + "&pageId=" + context.pageId;
               try {
                  TestTaskItem testItem = taskItemService.findByTestTaskIdAndTestPlanItemId(context.testTaskId, context.testPlanItemId);
                  testItem.setStartTime(new Date());
                  taskItemService.update(testItem);
                  Process p = Runtime.getRuntime().exec(
                           "c:/Windows/system32/mshta.exe " + url);
                  while (context.ideState == AtCommand.ideRunning) {
                      context.wait();
                  }
                  p.destroy();
                  testItem = taskItemService.findByTestTaskIdAndTestPlanItemId(context.testTaskId, context.testPlanItemId);
                  testItem.setEndTime(new Date());
                  taskItemService.update(testItem);
                  sleep(1000);
               } catch (Exception e) {
                 e.printStackTrace();
               }

           }
       }
    }
}
